/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kasgroup2;

import java.awt.Component;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author beha
 */
public class CoreHandler {
    
    private static String dateFormat = "dd-MM-yyyy hh:mm";
    
    private ArrayList<Speaker> speakers = null;
    private ArrayList<Hotel> hotelList = null;
    private ArrayList<Conference> conferenceList = null;
    private ArrayList<Activity> activityList = null;
    
    
    public static String getDateFormat() {
        return dateFormat;
    }
    
    public static Date StringToDate(String date) {
        Date d = new Date();
        try {
            d = SimpleDateFormat.getInstance().parse(date);
        } catch (ParseException ex) {
            
        }
        return d;
    }
    
    public static String DateToString(Date p_Date) {
        return new SimpleDateFormat(dateFormat).format(p_Date);
    }
    
    public ArrayList<Hotel> getHotelList() {
        return hotelList;
    }
        
    public ArrayList<Conference> getConferenceList() {
        return conferenceList;
    }
    
    public ArrayList<Activity> getActivityList() {
        return activityList;
    }
    
    public ArrayList<Speaker> getSpeakerList() {
        return speakers;
    }
    
    /*public ArrayList<Speaker> getSpeakerList() {
        ArrayList<Speaker> speakerList = new ArrayList<Speaker>();
        for(Person p : personList) {
            if(Person.IsSpeaker(p)) {
                speakerList.add((Speaker)p);
            }
        }
        return speakerList;
    }
    
    public ArrayList<Participant> getParticipantList() {
        ArrayList<Participant> participantList = new ArrayList<Participant>();
        for(Person p : personList) {
            if(Person.IsParticipant(p)) {
                participantList.add((Participant)p);
            }
        }
        return participantList;
    }
    
    public ArrayList<Companion> getCompanionList() {
        ArrayList<Companion> companionList = new ArrayList<Companion>();
        for(Person p : personList) {
            if(Person.IsCompanion(p)) {
                companionList.add((Companion)p);
            }
        }
        return companionList;
    }*/
    
    public void addActivity(Activity a) {
        this.activityList.add(a);
    }
    
    public void updateActivity(Activity newActivity) {
        for(Activity a : this.activityList) {
            if(a.getName().equals(newActivity.getName())) {
                a = newActivity;
                break;
            }
        }
    }
    
    public void updateHotel(Hotel newHotel) {
        for(Hotel h : this.hotelList) {
            if(h.getID() == newHotel.getID()) {
                h = newHotel;
                break;
            }
        }
    }
    
    public void addHotel(Hotel h) {
        this.hotelList.add(h);
    }
    
    public void addConference(Conference c) {
        this.conferenceList.add(c);
    }
    
    public void addSpeaker(Speaker s) {
        this.speakers.add(s);
    }

    public void updateSpeaker(Speaker p_Speaker, ArrayList<Subject> p_Subjects) {
        for(Speaker s : this.speakers) {
            if(s.getName().equals(p_Speaker.getName())) {
                s = p_Speaker;
                s.addSubjects(p_Subjects);
            }
        }
    }
    
    /*public void addPerson(Person p) {
        this.personList.add(p);
    }*/
    
    public ArrayList<Conference> getConferenceList(String state) {
        ArrayList<Conference> conferenceListTemp = null;
        
        if(EnumConferenceState.valueOf(state) == EnumConferenceState.ALL) {
            conferenceListTemp = conferenceList;
        }
        else if(EnumConferenceState.valueOf(state) == EnumConferenceState.ACTIVE) {
            conferenceListTemp = getConferences(true);
        }
        else if(EnumConferenceState.valueOf(state) == EnumConferenceState.INACTIVE) {
            conferenceListTemp = getConferences(false);
        }
        return conferenceListTemp;
    }
    
    public ArrayList<Conference> getConferences(Boolean isActive) {
        ArrayList<Conference> conferenceListTemp = new ArrayList<Conference>();
        for(Conference c : conferenceList) {
            if(isActive == c.getIsActive()) {
                conferenceListTemp.add(c);
                System.out.println(isActive);
            }
        }
        return conferenceListTemp;
    }
    
    public ArrayList<Subject> getSpeakerSubjects(String p_SpeakerName) {
        for(Speaker s : getSpeakerList()) {
            if(s.getName().equals(p_SpeakerName)) {
                return s.getSubjectList();
            }
        }
        return null;
    }
    /*public ArrayList<Subject> getSpeakerSubjects(String p_SpeakerName) {
        for(Speaker s : getSpeakerList()) {
            if(s.getName().equals(p_SpeakerName)) {
                return s.getSubjectList();
            }
        }
        return null;
    }*/
    
    public Conference getConference(String p_ConferenceName) {
        for(Conference c : conferenceList) {
            if(c.getName().equals(p_ConferenceName)) {
                return c;
            }
        }
        return null;
    }
    
    public Speaker getSpeaker(Object p_SpeakerName) {
        if(p_SpeakerName != null) {
            for(Speaker s : getSpeakerList()) {
                if(s.getName().equals(p_SpeakerName.toString())) {
                    return s;
                }
            }
        }
        return null;
    }

    public Speaker getSpeaker(int index) {
        ArrayList<Speaker> sl = getSpeakerList();
        if(index > 0 && index <= sl.size()) {
            return getSpeakerList().get(index);
        }
        return null;
    }
    
    /*public Person getPerson(String p_PersonName) {
        for(Person p : this.personList) {
            if(p.getName().equals(p_PersonName)) {
                return p;
            }
        }
        return null;
    }
    
    public Speaker getSpeaker(String p_SpeakerName) {
        for(Speaker s : getSpeakerList()) {
            if(s.getName().equals(p_SpeakerName)) {
                return s;
            }
        }
        return null;
    }

    public Speaker getSpeaker(int index) {
        return getSpeakerList().get(index);
    }
    
    public Participant getParticipant(String p_Participantt) {
        for(Participant p : getParticipantList()) {
            if(p.getName().equals(p_Participantt)) {
                return p;
            }
        }
        return null;
    }*/
    
    public CoreHandler() {
        speakers = new ArrayList<Speaker>();
        hotelList = new ArrayList<Hotel>();
        conferenceList = new ArrayList<Conference>();
        activityList =  new ArrayList<Activity>();
    }
    
    public Boolean processRegistration(
                                        String p_Name, 
                                        String p_Address, 
                                        String p_City, 
                                        String p_Country,
                                        String p_FirmName,
                                        String p_PhoneFirm,
                                        String p_PhoneHome,
                                        String p_ArrivalDate,
                                        String p_DepartureDate,
                                        Object p_ConferenceName,
                                        Boolean p_HasCompanion,
                                        Boolean p_IsSpeaker,
                                        Boolean p_WantsHotel,
                                        String p_CompanionName,
                                        TableManager p_Activities,
                                        ArrayList<HotelRoomGuest> p_HotelRooms) 
    {        
        if(p_ConferenceName == null) {
            MessageBox(null, "No conference", "There has not been created any conferences yet!");
            return false;
        }
        
        Conference selConference = getConference(p_ConferenceName.toString());
        
        ArrayList<Activity> companionActivities = new ArrayList<Activity>();
        
        for(int i : p_Activities.getSelectedRows()) {            
            String name = (String)p_Activities.getCellValue(i, 0);
            String place = (String)p_Activities.getCellValue(i, 1);
            Date from = StringToDate(p_Activities.getCellValue(i, 2).toString());
            Date to = StringToDate(p_Activities.getCellValue(i, 3).toString());
            Double price = (Double)p_Activities.getCellValue(i, 4);
            String comment = (String)p_Activities.getCellValue(i, 5);
            
            Activity activity = new Activity(name, place, price, comment, from, to);
            companionActivities.add(activity);
            //System.out.println(i + " : " +  +  "\n");
        }
        
        Companion companion = null;
        if (p_HasCompanion) {
            companion = new Companion(p_CompanionName, companionActivities);
        }
        
        Date arrivalDate = StringToDate(p_ArrivalDate);
        Date departureDate = StringToDate(p_DepartureDate);
        
        Participant participant = new Participant(
                p_Name, 
                p_Address, 
                p_City, 
                p_Country, 
                p_FirmName, 
                p_PhoneFirm, 
                p_PhoneHome, 
                arrivalDate, 
                departureDate,
                companion);
        
        if(p_WantsHotel) {
            participant.addHotelRooms(p_HotelRooms);
        }
        /*for(int i : tmh.getSelectedRows()) {
            participant.addHotel(getHotel(i).getID(), );
            System.out.println("Added hotel: " + getHotel(i).getID() + getHotel(i).getName() + " to guest: " + p_Name);
        }*/
        
        if(p_IsSpeaker) {
            participant = new Speaker(participant);
            addSpeaker((Speaker)participant);
        }
        selConference.addPerson(participant);
        /*for(Person p : personList) {
            Companion c = null;
            if(isParticipant(p)) {
                Participant p2 = (Participant)p;
                c = p2.getCompanion();
            }
            String companionName = "";
            if(c != null) {
                companionName = c.getName();
                companionName = " - companion: " + companionName;
            }
            System.out.println(p.getName() + companionName + " - isCompanion? " + isCompanion(p) + " - isSpeaker? " + isSpeaker(p));
        }*/
        
        return true;
    }

    void createConference(
            String p_Name, 
            Double p_Price,
            String p_Place,
            String p_Description,
            Boolean p_IsActive,
            String p_From, 
            String p_To, 
            int[] p_selectedHotels, 
            int[] p_SelectedActivities, 
            int[] p_SelectedSpeakers) {
        Date from = new Date();
        Date to = new Date();
        try {
            from = DateFormat.getInstance().parse(p_From);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        try {
            to = DateFormat.getInstance().parse(p_To);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        Conference conference = new Conference(p_Name, p_Price, p_Place, p_Description, from, to, p_IsActive);
        this.conferenceList.add(conference);
    }

    public void removeActivity(String p_ActivityName) {
        Iterator<Activity> it = this.activityList.iterator();
        while(it.hasNext()) {
            if(it.next().getName().equals(p_ActivityName)) {
                it.remove();
            }
        }
    }

    public void removeHotel(String p_HotelName) {
        Iterator<Hotel> it = this.hotelList.iterator();
        while(it.hasNext()) {
            if(it.next().getName().equals(p_HotelName)) {
                it.remove();
            }
        }
    }
    
    public static Boolean ConfirmDialog(Component c, String title, String message) {
        Object[] options = new Object[] { "Yes", "No" };
        int result = JOptionPane.showOptionDialog(
                        c, 
                        message, 
                        title, 
                        JOptionPane.YES_NO_OPTION, 
                        JOptionPane.QUESTION_MESSAGE, 
                        null, 
                        options, 
                        options[0]);
        if(result == 0) {
            return true;
        }
        return false;
    }
    
    public static void MessageBox(Component c, String title, String message) {
        Object[] options = new Object[] { "Ok" };
        int result = JOptionPane.showOptionDialog(
                        c, 
                        message, 
                        title, 
                        JOptionPane.OK_OPTION, 
                        JOptionPane.INFORMATION_MESSAGE, 
                        null, 
                        options, 
                        options[0]);
    }
    
    public Boolean removeSpeaker(String p_SpeakerName) {
        Speaker s = getSpeaker(p_SpeakerName);
        if(s != null) {
            speakers.remove(s);
            return true;
        }
        return false;
    }

    /*public Boolean removePerson(String p_PersonName) {
        Person person = getPerson(p_PersonName);
        if(person != null) {
            this.personList.remove(person);
            return true;
        }
        return false;
    }*/

    public void editConference(
            String p_ConferenceName, 
            String p_Description,
            String p_Place,
            String p_Start,
            String p_End,
            Boolean p_IsActive,
            ArrayList<Speaker> p_SpeakerList,
            ArrayList<Activity> p_ActivityList) {
        
        Conference c = getConference(p_ConferenceName);
        c.setDescription(p_Description);
        c.setPlace(p_Place);
        c.setStart(StringToDate(p_Start));
        c.setEnd(StringToDate(p_End));
        c.setIsActive(p_IsActive);
        c.updateSpeakers(p_SpeakerList);
        c.setActivityList(p_ActivityList);
    }

    public Hotel getHotel(int i) {
        return this.hotelList.get(i);
    }
    
    public Hotel getHotel(Object p_HotelName) {
        if(p_HotelName != null) {
            for(Hotel h : this.hotelList) {
                if(h.getName().equals(p_HotelName.toString())) {
                    return h;
                }
            }
        }
        return null;
    }
    
    public Activity getActivity(Object p_ActivityName) {
        if(p_ActivityName != null) {
            for(Activity a : this.activityList) {
                if(a.getName().equals(p_ActivityName.toString())) {
                    return a;
                }
            }
        }
        return null;
    }
    
    public Room getRoom(String p_HotelName, Object p_RoomType) {
        if(p_RoomType != null) {
            EnumRoomType selRoomType = EnumRoomType.valueOf(p_RoomType.toString());
            Iterator it = getHotel(p_HotelName).getRooms().entrySet().iterator();
            while(it.hasNext()) {
                Map.Entry pairs = (Map.Entry)it.next();
                Room r = (Room)pairs.getKey();
                if(r.getRoomType().equals(selRoomType)) {
                    return r;
                }
            }
        }
        return null;
    }
    
    
}
